package com.yy.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;  


/**
 * 日期工具类
 * @author 
 */
public class DateUtils {  
	/** 时间格式(yyyy) */
    public static final String YEAR_DATE_FORMAT = "yyyy";
    /** 时间格式(MM) */
    public static final String SIMPLE_MONTH_FORMAT = "MM";
    /** 时间格式(yyyy-MM) */
    public static final String MONTH_DATE_FORMAT = "yyyy-MM"; 
    /** 时间格式(yyyy-MM-dd) */
    public static final String SIMPLE_DATE_FORMAT = "yyyy-MM-dd";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /** 时间格式(yyyyMM) */
    public static final String MONTH_DATE = "yyyyMM";
    /** 时间格式(yyyyMMdd) */
    public static final String FORMAT_DATE = "yyyyMMdd";
    
	public static String format(Date date) {
        return format(date, SIMPLE_DATE_FORMAT);
    }

    public static String format(Date date, String pattern) {
        if(date != null){
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }
    
    public static String toDateStr(Date date) {
        return format(date,SIMPLE_DATE_FORMAT);
    }
    
    public static String toDateTimeStr(Date date) {
        return format(date,DATE_TIME_FORMAT);
    }
    
    public static String toYearStr(Date date) {
        return format(date,YEAR_DATE_FORMAT);
    }
    
    public static String toSimpleMonthStr(Date date) {
        return format(date,SIMPLE_MONTH_FORMAT);
    }
    
    public static String toMonthStr(Date date) {
        return format(date,MONTH_DATE_FORMAT);
    }
    
    public static String currentDateStr() {
        return format(new Date(),SIMPLE_DATE_FORMAT);
    }
    
    public static String currentDateTimeStr() {
        return format(new Date(),DATE_TIME_FORMAT);
    }
    
    public static String currentYearStr() {
        return format(new Date(),YEAR_DATE_FORMAT);
    }
    public static String currentSimpleMonthStr() {
        return format(new Date(),SIMPLE_MONTH_FORMAT);
    }
    public static String currentMonthStr() {
        return format(new Date(),MONTH_DATE_FORMAT);
    }
    public static String toMonthString(Date date) {
        return format(date,MONTH_DATE);
    }
    public static String currentMonthString() {
        return format(new Date(),MONTH_DATE);
    }
    public static String toFormatString(Date date) {
        return format(date,FORMAT_DATE);
    }
    public static String currentFormatString() {
        return format(new Date(),FORMAT_DATE);
    }
    
    /** 
     * 获得服务器当前日期，以格式为：yyyy-MM-dd的日期字符串形式返回 
     */  
    public static String getDateStr() {  
        try {  
            return new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime());  
        } catch (Exception e) {  
            return "";  
        }  
    }  
    /** 
     * 返回日期加X天后的日期 
     * @param date 
     * @param days
     * @return 
     */  
    public static Date addDay(Date date, int days) {  
        try {  
        	Calendar c = Calendar.getInstance();  
            c.setTime(date);  
            c.add(Calendar.DAY_OF_YEAR, days);  
            return c.getTime();  
        } catch (Exception e) {  
            return date;  
        }  
    }  
  
    /** 
     * 返回日期加X月后的日期 
     * @param date 
     * @param months 
     * @return 
     */  
    public static Date addMonth(Date date, int months) {  
        try {  
        	Calendar c = Calendar.getInstance();  
            c.setTime(date);  
            c.add(Calendar.MONTH, months); 
            return c.getTime();   
        } catch (Exception e) {  
            return date;  
        }  
    }  
  
    /** 
     * 返回日期加X年后的日期 
     * @param date 
     * @param years 
     * @return 
     */  
    public static Date addYear(Date date, int years) {  
        try {  
        	 Calendar c = Calendar.getInstance();  
             c.setTime(date);  
             c.add(Calendar.YEAR, years);  
             return c.getTime();    
        } catch (Exception e) {  
            return date;  
        }  
    }  

}  